package team.nmsg.ge.system.init.shiro;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import team.nmsg.ge.system.AppServerConstant;
import team.nmsg.ge.system.AppServerParams;

@Configuration
//@EnableCaching
public class EhCacheConfiguration {
	
	/**
	 * ehcache 主要的管理器
	 * 
	 * @param bean
	 * @return
	 */
	@Bean
	public EhCacheCacheManager ehCacheCacheManager( EhCacheManagerFactoryBean bean) {
		EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager( bean.getObject());
		CacheManager cacheManager = ehCacheCacheManager.getCacheManager();
		
		//(String name, int maxElementsInMemory, boolean overflowToDisk, boolean eternal, long timeToLiveSeconds, long timeToIdleSeconds)
		Cache tokenCache = new Cache( AppServerConstant.NMS_TOKEN_CACHE, 500, true, false, AppServerParams.TOKEN_REFRESH_TIME, AppServerParams.TOKEN_REFRESH_TIME);
		cacheManager.addCache(tokenCache);
		
		return ehCacheCacheManager;
	}

    /**
     * 据shared与否的设置,
     * Spring分别通过CacheManager.create()
     * 或new CacheManager()方式来创建一个ehcache基地.
     *
     * 也说是说通过这个来设置cache的基地是这里的Spring独用,还是跟别的(如hibernate的Ehcache共享)
     *
     */
    @Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
      EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean ();
      cacheManagerFactoryBean.setConfigLocation (new ClassPathResource("ehcache.xml"));
      cacheManagerFactoryBean.setShared(true);
      return cacheManagerFactoryBean;
    }


    
    
	
}
